package com.lvy.hczn.front.business.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lvy.hczn.front.business.common.CacheConstants;
import com.lvy.hczn.front.system.domain.business.OutboundOrder;
import com.lvy.hczn.front.business.dto.Param;
import com.lvy.hczn.front.business.service.OutboundOrderDetailService;
import com.lvy.hczn.front.business.service.OutboundOrderService;
import com.lvy.hczn.front.business.util.ResultUtil;
import com.lvy.hczn.front.common.core.redis.RedisCache;
import com.lvy.hczn.front.common.exception.UtilException;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/wcs/out/")
@Slf4j
public class OutboundOrderController {

    @Autowired
    private OutboundOrderService outboundOrderService;

    @Autowired
    private OutboundOrderDetailService outboundOrderDetailService;

    @Autowired
    private RedisCache redisCache;
    
    /**
     * 根据未出库出库单查询
     *
     * @param param
     * @return
     */
    @ApiOperation(value = "根据未出库出库单查询", notes = "根据未出库出库单查询")
    @PostMapping("selectByCode")
    public ResultUtil selectByCode(@Valid @RequestBody Param param) {
        try {
            String outboundCode = param.getUpOrDownPolicy().getOutboundOrderCode();
            if (StrUtil.isEmpty(outboundCode)) {
                return ResultUtil.failed("出库单为空");
            }
            OutboundOrder outboundOrder = outboundOrderService.getOne(Wrappers.<OutboundOrder>lambdaQuery().eq(OutboundOrder::getOutboundOrderCode, outboundCode));
            if (outboundOrder == null) {
                return ResultUtil.failed("信息不存在");
            }

            /*List<OutboundOrderDetail> list = outboundOrderDetailService.list(Wrappers.<OutboundOrderDetail>lambdaQuery().eq(OutboundOrderDetail::getOutboundOrderCode, outboundCode));
            if (list == null || list.isEmpty()) {
                return ResultUtil.failed("无出库信息");
            }
            return ResultUtil.ok(list);*/
            return ResultUtil.ok(outboundOrder);
        } catch (Exception e) {
            log.error("selectByCode:{}", e);
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }

    @ApiOperation(value = "查询操作人员是否有出库单", notes = "查询操作人员是否有出库单")
    @PostMapping("selectOutboundCodeByClient")
    public ResultUtil selectOutboundCodeByClient(@RequestBody Param param) {
        try {
            //todo clientId 校验
            String key = CacheConstants.REDIS_KEY_SOCKET_CLIENT + ":" + param.getUpOrDownPolicy().getClientId() + ":outboundCode";
            String result = (String) redisCache.get(key);
            if (StrUtil.isEmpty(result)) {
                return ResultUtil.failed();
            }
            return ResultUtil.ok(result);
        } catch (Exception e) {
            log.error("selectOutboundCodeByClient:{}", e);
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }

    @ApiOperation(value = "清除操作人员的出库单绑定", notes = "清除操作人员的出库单绑定")
    @PostMapping("clearOutboundCodeByClient")
    public ResultUtil clearOutboundCodeByClient(@RequestBody Param param) {
        try {
            outboundOrderService.clearOutboundCodeByClient(param);
            return ResultUtil.ok();
        } catch (Exception e) {
            log.error("clearOutboundCodeByClient:{}", e);
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }
}
